<template>
  <div class="Associated_Field">
    <div class="pubBig Privilege">
      <div class="h0">
        <p class="lt"></p>
      </div>
      <div class="dDownMain">
        <div
          class="twoBox"
          :style="this.$route.query.Token ? 'top:50px;left:10px':'top: 100px;padding:10px'"
        >
          <el-row>
            <el-col :span="12">
              <el-button
                size="small"
                @click="fieldType='',getTableData()"
                :type="fieldType == '' ? 'primary':''"
              >全部</el-button>
              <el-button
                size="small"
                @click="fieldType='Character',getTableData()"
                :type="fieldType == 'Character' ? 'primary':''"
              >Character</el-button>
              <el-button
                size="small"
                @click="fieldType='Number',getTableData()"
                :type="fieldType == 'Number' ? 'primary':''"
              >Number</el-button>
              <el-button
                size="small"
                @click="fieldType='Time',getTableData()"
                :type="fieldType == 'Time' ? 'primary':''"
              >Time</el-button>
              <el-button
                size="small"
                @click="fieldType='Date',getTableData()"
                :type="fieldType == 'Date' ? 'primary':''"
              >Date</el-button>
              <el-button
                size="small"
                @click="fieldType='Voolean',getTableData()"
                :type="fieldType == 'Voolean' ? 'primary':''"
              >Voolean</el-button>
            </el-col>
            <el-col :span="12">
              <el-button
                style="float:right;margin-right:5px"
                size="small"
                icon="el-icon-plus"
                @click="open"
              >新增字段</el-button>
              <el-button
                size="small"
                style="float:right;margin-right:5px"
                icon="el-icon-search"
                @click="page.page=1,getTableData()"
              ></el-button>
              <el-input
                size="small"
                style="display:inline-block;width:30%;float:right;margin-right:5px"
                placeholder="请输入字段英文名/字段中文名查询"
                v-model="search_val"
              ></el-input>
            </el-col>
          </el-row>
          <template>
            <el-table
              :data="tableData"
              style="width: 100%;margin-top:10px"
              height="600"
            >
              <el-table-column
                prop="CnName"
                label="字段中文名"
              >
              </el-table-column>
              <el-table-column
                prop="EnName"
                label="字段英文名"
              >
              </el-table-column>
              <el-table-column
                prop="FieldType"
                label="字段类型"
              >
              </el-table-column>
              <el-table-column
                prop="FieldDesc"
                label="描述"
              >
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    icon="el-icon-edit"
                    @click="edit(scope.row)"
                  ></el-button>
                  <el-button
                    size="mini"
                    icon="el-icon-delete"
                    @click="del(scope.row.Id)"
                  ></el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
          <div
            class="block"
            style="text-align:center"
          >
            <el-pagination
              layout="prev, pager, next"
              :total="page.total"
              :page-size="page.limit"
              @current-change="page_change"
            >
            </el-pagination>
          </div>
        </div>
        <!-- 弹层 -->
        <el-dialog
          :title="title"
          :visible.sync="dialogVisible"
          width="45%"
          :before-close="handleClose"
          :close-on-click-modal="false"
          :modal-append-to-body="false"
        >
          <el-form
            label-position="right"
            label-width="120px"
            :model="formLabelAlign"
          >
            <el-form-item
              label="字段中文名"
              style="width:50%;display:inline-block"
            >
              <el-input v-model="formLabelAlign.CnName"></el-input>
            </el-form-item>
            <el-form-item
              label="字段英文名"
              style="width:50%;display:inline-block"
            >
              <el-input v-model="formLabelAlign.EnName"></el-input>
            </el-form-item>
            <el-form-item
              label="字段类型"
              style="width:50%;display:inline-block"
            >
              <el-select
                filterable
                v-model="formLabelAlign.FieldType"
                placeholder="请选择"
                style="width:100%"
              >
                <el-option
                  label="Character"
                  value="Character"
                >
                </el-option>
                <el-option
                  label="Number"
                  value="Number"
                >
                </el-option>
                <el-option
                  label="Time"
                  value="Time"
                >
                </el-option>
                <el-option
                  label="Date"
                  value="Date"
                >
                </el-option>
                <el-option
                  label="Voolean"
                  value="Voolean"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="描述"
              style="width:50%;display:inline-block"
            >
              <el-input v-model="formLabelAlign.FieldDesc"></el-input>
            </el-form-item>
          </el-form>
          <span
            slot="footer"
            class="dialog-footer"
          >
            <el-button @click="handleClose">取 消</el-button>
            <el-button
              type="primary"
              @click="btn_OK"
            >确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  data () {
    return {
      tableData: [],
      search_val: '',
      fieldType: '',
      page: {
        page: 1,
        limit: 10,
        total: 1
      },
      dialogVisible: false,
      formLabelAlign: {},
      title: ''
    };
  },
  methods: {
    getTableData () {// 获取表格数据
      this.$post("/ServiceManage/Field/PageList?keyword=" + this.search_val + "&fieldType=" + this.fieldType + "&page=" + this.page.page + "&limit=" + this.page.limit).then(res => {
        if (res.code == 0) {
          this.tableData = res.data.list
          this.page.total = res.data.TotalCount
        }
      })
    },
    page_change (val) { // 页码切换事件
      this.page.page = val
      this.getTableData()
    },
    open () {// 弹层开启方法
      this.formLabelAlign.Id ? this.title = '修改' : this.title = '新增'
      this.dialogVisible = true
    },
    handleClose () { // 弹层关闭事件
      this.dialogVisible = false
      this.formLabelAlign = {}
    },
    btn_OK () { // 弹层确定按钮点击事件
      let url = this.formLabelAlign.Id ? '/ServiceManage/Field/Update' : '/ServiceManage/Field/Create'
      this.$post(url, this.formLabelAlign).then(res => {
        if (res.code == 0) {
          this.getTableData()
          this.handleClose()
          this.$message({
            message: '操作成功',
            type: 'success'
          });
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      }).catch(err => {
        this.$message({
          message: '出现错误',
          type: 'error'
        });
      })
    },
    edit (val) { // 表格编辑事件
      var info = JSON.stringify(val)
      this.formLabelAlign = JSON.parse(info)
      this.open()
    },
    del (id) { // 表格删除事件
      this.$confirm("确定删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(_ => {
          this.$post("/ServiceManage/Field/Delete/" + id).then(res => {
            if (res.code == 0) {
              this.getTableData()
              this.$message({
                message: '操作成功',
                type: 'success'
              });
            } else {
              this.getTableData()
              this.$message({
                message: res.msg,
                type: 'error'
              });
            }
          }).catch(err => {
            this.$message({
              message: '出现错误',
              type: 'error'
            });
          })
        })
        .catch(_ => { });
    }
  },
  mounted () {
    // document.getElementById("2").focus();
    this.getTableData()
  },
  created () {
    if (this.$route.query.Token) {
      localStorage.setItem('Token', this.$route.query.Token)
    }
  }
};
</script>
<style lang="scss">
.Associated_Field {
  .top-input {
    display: inline-block;
    width: 350px;
  }

  .box-top {
    margin-top: 15px;
    margin-left: 5px;
  }
}
</style>
